package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 * Builds a JFrame window with option settings for the effort calculating.
 */
public class OptionsWindow extends JFrame {
	private int estUnit;
	private JTextField tf;
	private GUI gui;

	public OptionsWindow(GUI gui) {
		super("Options");
		this.gui = gui;
		estUnit = gui.getEstUnit();
		initOW();
		
	}

	/**
	 * Sets up the window content.
	 */
	public void initOW() {
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(4, 4, 4, 4);
		
		JPanel p1 = new JPanel();
		JLabel l = new JLabel("Similarity threshold:");
		tf = new JTextField(Integer.toString(gui.aesp.getThreshold()),6);
		tf.setHorizontalAlignment(JTextField.RIGHT);
		p1.add(l);
		p1.add(tf);
		
		JRadioButton rb1 = new JRadioButton("Person hours (ph)");
		JRadioButton rb2 = new JRadioButton("Person days (pd)");
		JRadioButton rb3 = new JRadioButton("Person months (pm)");
		JRadioButton rb4 = new JRadioButton("Person years (py)");
		ButtonGroup bg = new ButtonGroup();
		bg.add(rb1);
		bg.add(rb2);
		bg.add(rb3);
		bg.add(rb4);
		
		switch (gui.getEstUnit()) {
		case 1:
			rb1.setSelected(true);
			break;
		case 2:
			rb2.setSelected(true);
			break;
		case 3:
			rb3.setSelected(true);
			break;
		case 4:
			rb4.setSelected(true);
			break;
		}
			
		rb1.addActionListener(radioButtonListener(1));
		rb2.addActionListener(radioButtonListener(2));
		rb3.addActionListener(radioButtonListener(3));
		rb4.addActionListener(radioButtonListener(4));
		
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(0,1));
		p2.add(rb1);
		p2.add(rb2);
		p2.add(rb3);
		p2.add(rb4);
		
		JButton b1 = new JButton("Done");
		b1.addActionListener(closeButtonListener(1));
		
		JButton b2 = new JButton("Cancel");
		b2.addActionListener(closeButtonListener(2));
		
		c.gridy = 1;
		c.gridx = 1;
		add(p1, c);
		c.gridy = 2;
		add(p2, c);
		c.gridy = 3;
		c.insets = new Insets(4, 40, 4, 40);
		c.anchor = GridBagConstraints.CENTER;
		add(b1, c);
		c.anchor = GridBagConstraints.BASELINE_LEADING;
		c.gridx = 2;
		add(b2, c);
		
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		pack();
		setLocationRelativeTo(this.getParent());
		setVisible(true);
	}

	/**
	 * ActionListener for the radio buttons 
	 */
	private ActionListener radioButtonListener(final int i) {
		ActionListener a = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				estUnit = i;
			}
		};
		return a;
	}
	
	/**
	 * ActionListener for the done and cancel buttons
	 */
	private ActionListener closeButtonListener(final int i) {
		ActionListener a = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				/*
				 * Done Button
				 */
				if (i == 1) {
					gui.setEstUnit(estUnit);
					String s = tf.getText();
					try {
						gui.aesp.setThreshold(getThreshold(s));
						dispose();
					} catch (IllegalArgumentException e2) {
						JFrame frame = new JFrame();
						JOptionPane.showMessageDialog(frame,
								"Threshold have to be a numerical value between 0-100", 
								"Message", JOptionPane.INFORMATION_MESSAGE);
					}
				/*
				 * Cancel button
				 */
				} else if (i == 2) {
					dispose();
				}
			}
		};
		return a;
	}
	
	/**
	 * Gets the threshold that the user types in. It has to be a value between
	 * 0-1.
	 */
	private int getThreshold(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		int d = -1;
		try {
			int d2 = Integer.parseInt(s);
			d = d2;
		} catch (NumberFormatException e) {
			throw new IllegalArgumentException();
		}
		if (d < 0 || d > 100) {
			throw new IllegalArgumentException();
		}
		return d;
	}
}
